// pages/other//redbag/index.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    jx_goods: [0, 1, 2, 3],
    timeline: '00:00:00',
    timer: '', //定时器名字
    button_state: '',
    content: '',
    goods: '',
    goods_id:'',
    url_unionid:'',
    zl:'',
    xr:'',
    people:'',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '加载中',
    })
    const that = this;
    that.setData({
      goods_id: options.goods_id,
      url_unionid:options.openid
    })
    var sign = app.sort_ASCII();
    wx.request({
      url: app.globalData.new_url + '/goods/Goods/goods_list',
      data:{
        sign:sign
      },
      method: 'POST',
      success: function (res) {
        console.log(res)
        that.setData({
          goods: res.data.content
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    const that=this;
    var goods_id = that.data.goods_id;
    var url_unionid = that.data.url_unionid;
    var object = {
      unionid: wx.getStorageSync('openid'),
      goods_id: goods_id ,
      url_unionid: url_unionid,
    }
    var sign = app.sort_ASCII(object)
    wx.request({
      url: app.globalData.new_url + '/goods/Goods/help_goods_info',
      data: {
        unionid: wx.getStorageSync('openid'),
        goods_id: goods_id,
        url_unionid: url_unionid,
        sign: sign
      },
      method: 'POST',
      success: function (res) {
        wx.hideLoading();
        console.log(res)
        if (res.data.code === 200) {
          if (res.data.content.time_data) {
            that.countDown();
          }
          var people = res.data.content.goods_zl_num - res.data.content.zl_count;
          that.setData({
            people:people,
            content: res.data.content,
            button_state: res.data.button_state,
          })
          if (people <= 0) {
            that.setData({
              people: 0
            })
          }
        } else {
          that.setData({
            button_state: res.data.button_state,
          })
        }
      }
    })
  },
  helpfriend:function(){
    const that=this;
    var goods_id = that.data.goods_id;
    var url_unionid = that.data.url_unionid;
    var object = {
      unionid: wx.getStorageSync('openid'),
      goods_id: goods_id,
      url_unionid: url_unionid,
    }
    var sign = app.sort_ASCII(object)
    wx.request({
      url: app.globalData.new_url + '/goods/Goods/help',
      data: {
        unionid: wx.getStorageSync('openid'),
        goods_id: goods_id,
        url_unionid: url_unionid,
        sign: sign
      },
      method: 'POST',
      success: function (res) {
        if(res.data.code===200){
          wx.showModal({
            title:'提示',
            content: '助力成功',
            showCancel:false,
            success(res) {
              if (res.confirm) {
                that.onShow()
              } 
            }
          })
        }
      }
    })
  },
  countDown: function() {
    const that = this;
    var timenow = Number(that.data.content.time_data);
    let second = timenow - (Date.parse(new Date()) / 1000); //获取倒计时初始值
    that.setData({
      timer: setInterval(function() {
        second--;
        // 小时位   
        var hr = Math.floor(second / 3600);
        var hrStr = hr.toString();
        if (hrStr.length == 1) hrStr = '0' + hrStr;
        // 分钟位  
        var min = Math.floor(second / 60 % 60);
        var minStr = min.toString();
        if (minStr.length == 1) minStr = '0' + minStr;
        // 秒位  
        var sec = Math.floor(second % 60);
        var secStr = sec.toString();
        if (secStr.length == 1) secStr = '0' + secStr;
        var timeline = hrStr + ":" + minStr + ":" + secStr;
        that.setData({
          timeline: timeline
        })
        if (second <= 0) {
          clearInterval(that.data.timer);
          that.onShow();
        }
      }, 1000)
    })
  },
  onHide: function() {
    clearInterval(this.data.timer);
  },
  onUnload: function() {
    clearInterval(this.data.timer);
  },
  freeDetail: function (e) {
    var id = e.target.dataset.id;
    wx.redirectTo({
      url: '../../main/oneDetail/index?id=' + id+ '&&parements=mm',
    })
  },
  goindex: function() {
    wx.switchTab({
      url: '/pages/tarbar/index/index',
    })
  },
  togoods:function(){
    const that=this;
    var id=that.data.goods_id;
    wx.redirectTo({
      url: '/pages/main/oneDetail/index?id=' + id +'&&parements=mm',
    })
  },
})